package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentInfoMapper paymentInfoMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private RabbitService rabbitService;

    //保存支付信息
    @Override
    public void savePaymentInfo(Long orderId, PaymentType paymentType) {

        //幂等性校验
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("order_id", orderId)
                .eq("payment_type", paymentType.getComment()));
        if (paymentInfo != null){
            return;
        } else {
            paymentInfo = new PaymentInfo();
            OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
            //对外业务编号
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            //订单编号
            paymentInfo.setOrderId(orderId);
            //支付类型
            paymentInfo.setPaymentType(paymentType.getComment());
            //交易编号
            //支付金额
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            //交易内容
            paymentInfo.setSubject(orderInfo.getTradeBody());
            //支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            //创建时间
            paymentInfo.setCreateTime(new Date());
            //回调时间
            //回调信息
            paymentInfoMapper.insert(paymentInfo);
        }

    }

    //根据orderId获取支付信息
   @Override
    public PaymentInfo getPaymentInfo(Long orderId) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>().eq("order_id",orderId));
    }

    //异步回调返回参数 更新支付信息
    @Override
    public void paySuccess(Map<String, String> paramsMap) {
        String out_trade_no = paramsMap.get("out_trade_no");
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", out_trade_no));
        //支付中 改成已经支付了
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        //银行流水
        String trade_no = paramsMap.get("trade_no");
        paymentInfo.setTradeNo(trade_no);
        //回调时间
        String notify_time = paramsMap.get("notify_time");
        try {
            paymentInfo.setCallbackTime(
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
                            parse(notify_time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //回调内容
        paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
        paymentInfoMapper.updateById(paymentInfo);
        //MQ发送消息 更新订单和库存
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

    }

    //获取退款金额
    @Override
    public BigDecimal getRefundAmount(String outTradeNo) {
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", outTradeNo));
        return paymentInfo.getTotalAmount();
    }
}
